package app.common.utils.history.year2024;

import app.common.utils.FormsUtils;
import app.common.utils.IdGenerator;
import app.common.utils.YdApi;
import app.common.utils.cjt.CJtUtils;
import app.common.utils.cjt.ao.OtherDispatchCreateAo;
import com.aliyun.dingtalkyida_1_0.models.BatchGetFormDataByIdListResponseBody;
import com.gitee.feizns.dynamic.DateUtils;
import com.gitee.feizns.dynamic.Lists;
import com.gitee.feizns.quickstart.jackson.JsonUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 宜搭货品出库单传畅捷通其他出库单
 * @author feizns
 * @since 2024/3/3
 */
public interface YiDaToCjtCkUtils {

    static void main(String[] args) {
        new Thread(() -> yiDaHpCkToCjtQtCk("2024-01-01", "2024-01-6")).start();
        new Thread(() -> yiDaHpCkToCjtQtCk("2024-01-06", "2024-01-11")).start();
        new Thread(() -> yiDaHpCkToCjtQtCk("2024-01-11", "2024-01-16")).start();
        new Thread(() -> yiDaHpCkToCjtQtCk("2024-01-16", "2024-01-21")).start();
        new Thread(() -> yiDaHpCkToCjtQtCk("2024-01-01", "2024-01-6")).start();
//        yiDaHpCkToCjtQtCk("2024-01-01", "2024-01-26");
    }

    /**
     * 宜搭货品出库单传畅捷通其他出库单
     */
    static void yiDaHpCkToCjtQtCk(String startDate, String endDate) {
        CJtUtils.resetOpenToken();
        String formUuid = "FORM-Q4A664A10E943MPDBSPU4AY3F4N32JOXNWJ8LH";
        //货品出库单传入畅捷通其他出库单
        List<String> ids = YdApi.yiDaUtils.forms.instanceIds(formUuid,
                startDate,
                endDate);
        List<List<String>> lists = Lists.partition(ids, 50);
        for (List<String> list : lists) {
            String[] partitionIds = list.toArray(new String[]{});
            List<BatchGetFormDataByIdListResponseBody.BatchGetFormDataByIdListResponseBodyResult> forms = YdApi.yiDaUtils.forms.getByIds(formUuid, partitionIds);
            for (BatchGetFormDataByIdListResponseBody.BatchGetFormDataByIdListResponseBodyResult data : forms) {
                Map<String, ?> formData = data.getFormData();
                //商家编码
                String no = String.valueOf(formData.get("textField_l8jwp3mz"));
                //单号
                String code = String.valueOf(formData.get("textField_ljjgry4w"));
                String originalFormName = String.valueOf(formData.get("textField_lg0ibky2"));
                String ckReason = String.valueOf(formData.get("textField_l8jwsgqf"));
                System.out.println(JsonUtils.toJson(data));
                OtherDispatchCreateAo.Dto dto = new OtherDispatchCreateAo.Dto();
                //外部编码 = 单号 + '-' + 商家编码
                dto.setExternalCode(code + "-" + no);
                dto.setCode(dto.getExternalCode());
                dto.setVoucherType(new OtherDispatchCreateAo.Dto.VoucherType("ST1024"));
                dto.setVoucherDate(data.getCreateTimeGMT());
                dto.setBusiType(new OtherDispatchCreateAo.Dto.BusiType("13"));
                dto.setWarehouse(new OtherDispatchCreateAo.Dto.Warehouse(String.valueOf(formData.get("textField_l8jwp3n3"))));
                dto.setMemo(
                        String.format("来源=[宜搭] 表单=[%s] 单号=[%s] 备注：[%s]",
                                originalFormName,
                                code,
                                ckReason
                        )
                );
                OtherDispatchCreateAo.Dto.RDRecordDetails details = new OtherDispatchCreateAo.Dto.RDRecordDetails();
                details.setInventory(new OtherDispatchCreateAo.Dto.RDRecordDetails.Inventory(no));
                double baseQuantity = Double.parseDouble(String.valueOf(formData.get("numberField_l8jwp3n1")));
                details.setBaseQuantity(BigDecimal.valueOf(baseQuantity));
                BigDecimal price = FormsUtils.getPrice(no);
                details.setPrice(price);
                details.setAmount(price.multiply(new BigDecimal(baseQuantity)));
                dto.setRDRecordDetailsObj(details);
                try {
                    CJtUtils.otherDispatchCreate(dto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 编号发电机
     */
    IdGenerator idGenerator = new IdGenerator();

    /**
     * 代码
     * @param date 日期
     * @param no 没有
     * @return {@link String}
     */
    static String code(String date, String no) {
        LocalDateTime time = DateUtils.parse(date, LocalDateTime.class);
        String key = no + "-" + time;
        return String.format("CK%s%05d", DateUtils.format(time, "yyyyMMddHHmm"), idGenerator.next(key));
    }

}
